var tuiwa = require("../../../tuiwa.js");
var questions = require("question");
var animation; // 动画实体
var ani_index = 0; // 动画执行次数index（当前执行了多少次）
var aniIntervalId = -1; // 动画定时任务id，通过setInterval来达到无限缩放，记录id，用于结束定时任务

Page({
  data: {
    titleIndexList: ["一", "二", "三", "四", "五", "六", "七", "八"],
    dengMiBg: "https://fcdn.qiushilaile.com/adx/adx-1951610445728862.png",
    index: 0,
    titleIndex: "一",
    content: "梅香时节转朱阁",
    remark: "(打一电影片名)",
    answer1: "复活",
    answer2: "白色情迷",
    answer3: "倒霉特工熊",
    answer4: "春归红楼",
    question_result: "倒霉特工熊",
    trueIndex: 0,
    hasSawList: [],
    showTrueResult: false,
    answerFailFlag: false,
    palyEndFlag: false,
    zheZhaoFlag: false,
  },
  onReady() {
    ani_index = 0;
    aniIntervalId = -1;
    this.data.animation = '';
    animation = tt.createAnimation({
      duration: 800,
      timingFunction: 'linear', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
      delay: 0, // 0延迟
      transformOrigin: '50% 50% 0'
    })
    this.startAnimationInterval()
  },
  aniScale: function (n) { // 缩放动画
    if (n % 2 == 0) {
      animation.scale(1.15).step();
    } else {
      animation.scale(1.0).step();
    }
    this.setData({
      animation: animation.export()
    });
  },
  startAnimationInterval: function () { // 开始缩放
    var that = this;
    that.aniScale(++ani_index); // 进行一次缩放
    aniIntervalId = setInterval(function () {
      that.aniScale(++ani_index);
    }, 800); // 每间隔0.8s进行一次缩放
  },
  back: function () {
    tt.navigateBack();
  },
  getNewQuestion: function () {

    let hasSawDengmiList = tt.getStorageSync('hasSawDengMiList');
    if (hasSawDengmiList == "") {
      hasSawDengmiList = []
    }

    let random = Math.floor(Math.random() * 50);
    let currentQuestion = questions.questions[random];

    //重置
    if (hasSawDengmiList.length > 45) {
      hasSawDengmiList = [];
      tt.setStorageSync('hasSawDengMiList', hasSawDengmiList);
    }

    //检测是否重复
    let repeatFlag = true;
    while (repeatFlag) {

      repeatFlag = false;
      for (let q of hasSawDengmiList) {
        if (q == currentQuestion.id) {
          repeatFlag = true;
          random = Math.floor(Math.random() * 50);
          currentQuestion = questions.questions[random];
          break;
        }
      }

    }
    hasSawDengmiList.push(currentQuestion.id)
    tt.setStorageSync('hasSawDengMiList', hasSawDengmiList);
    return currentQuestion;

  },
  onShow:function(options){
      tuiwa.globalData.currentPage = this;
      tt.setStorageSync('g_id', 'dengmi');
  },
  onLoad: function (options) {


    let question = this.getNewQuestion();


    this.setData({
      index: 0,
      titleIndex: this.data.titleIndexList[this.data.index],
      content: question.question_content,
      remark: question.question_remark,
      answer1: question.question_answer[0].answer_content,
      answer2: question.question_answer[1].answer_content,
      answer3: question.question_answer[2].answer_content,
      answer4: question.question_answer[3].answer_content,
      question_result: question.question_result,
    })




  },
  checkAnwser: function (e) {
    let answer = e.currentTarget.dataset.anwser;
    let index = e.currentTarget.dataset.index;

    if (answer == this.data.question_result) {
      this.nextQuestion()
    } else {
      this.setData({
        answerFailFlag: true,
        showTrueResult: true,
      })
    }

  },
  showPlayResult: function (e) {
    console.log(this.data.index, "index")
    this.setData({
      palyEndFlag: true
    })
  },

  nextQuestion: function (e) {
    let index = this.data.index + 1
    this.setData({
      index: index,
      trueIndex: 0,
      showTrueResult: false,
    })
    console.log(this.data.index)
    if (this.data.index >= 8) {
      this.showPlayResult();
    } else {


      let question = this.getNewQuestion();


      this.setData({
        index: index,
        titleIndex: this.data.titleIndexList[this.data.index],
        content: question.question_content,
        remark: question.question_remark,
        answer1: question.question_answer[0].answer_content,
        answer2: question.question_answer[1].answer_content,
        answer3: question.question_answer[2].answer_content,
        answer4: question.question_answer[3].answer_content,
        question_result: question.question_result,
      })
    }
  }
  ,
  reSetPlay: function (e) {
    //index 置为1，重新挑选测试题。
    this.setData({
      trueIndex: 0,
      index: 0,
      palyEndFlag: false,
      answerFailFlag: false,
      showTrueResult: false,
    })
    let question = this.getNewQuestion();

    this.setData({
      index: 0,
      titleIndex: this.data.titleIndexList[this.data.index],
      content: question.question_content,
      remark: question.question_remark,
      answer1: question.question_answer[0].answer_content,
      answer2: question.question_answer[1].answer_content,
      answer3: question.question_answer[2].answer_content,
      answer4: question.question_answer[3].answer_content,
      question_result: question.question_result,
    })

  },

  closeAdFunction: function (e) {


    let trueIndex = 0;

    if (this.data.question_result == this.data.answer1) {
      trueIndex = 1
    }
    if (this.data.question_result == this.data.answer2) {
      trueIndex = 2
    }
    if (this.data.question_result == this.data.answer3) {
      trueIndex = 3
    }
    if (this.data.question_result == this.data.answer4) {
      trueIndex = 4
    }

    this.setData({
      trueIndex: trueIndex
    })
    this.setData({
      answerFailFlag: false
    })
  },
  cancelAdFunction: function (e) {
    tt.showModal({
      title: "温馨提示",
      content: "要观看完整视频才能继续哦~",
      showCancel: !1
    })

  },
  watchVideo: function (e) {

    tuiwa.playAd();



  }

  ,
  goToIndex: function (e) {
    tt.redirectTo({
      url: '/pages/index/index' // 指定页面的url
    });
  }

})
